package com.west_traveler.find_page_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_category.EntityCategory;
import com.west_traveler.entity.entity_category.Item_Category;
import com.west_traveler.inteface.HttpService;
import com.west_traveler.recyclerview_adapter.CategoryRecyclerAdapter;
import com.west_traveler.ximalayafm.R;

import java.util.LinkedList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Description: 发现页分类Fragment
 * Date:2016年12月02日15:10
 *
 * @author: 刘悦.
 */

public class CategoryFragment extends Fragment implements Callback<EntityCategory> {


    private RecyclerView recycler;
    private HttpService service;
    private CategoryRecyclerAdapter adapter;

    public static CategoryFragment getInstance(){
        return new CategoryFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_category, container, false);

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler = (RecyclerView) view.findViewById(R.id.recycleview_category_Fra_id);
        //数据源
        BaseApplication application = (BaseApplication) getActivity().getApplication();
        service = application.getService();
        service.getCategory("and-a1","android",13,2).enqueue(this);

        adapter = new CategoryRecyclerAdapter(getContext());
        recycler.setAdapter(adapter);
    }


    @Override
    public void onResponse(Call<EntityCategory> call, Response<EntityCategory> response) {
        List<Item_Category> list = response.body().getList();
        adapter.addPaid(list.get(0));
        //数据重构
        List<EntityCategory> categorydatas = new LinkedList<>();
        for (int j = 0; j < (list.size()-1) / 6; j++) {
            EntityCategory entityCategory = new EntityCategory();
            List<Item_Category> item_list = new LinkedList<>();
            for (int i = 1; i <= 6; i++) {
                item_list.add(list.get(j*6+i));
            }
            entityCategory.setList(item_list);
            Log.i("100","entityCategory"+entityCategory.toString()+".............");
            categorydatas.add(entityCategory);
        }
        int sy = list.size()  - categorydatas.size() * 6;
        EntityCategory entityCategory = new EntityCategory();
        List<Item_Category> item_list = new LinkedList<>();
        for (int i = 1;i<sy;i++){
            item_list.add(list.get(categorydatas.size() * 6+i));
        }
        entityCategory.setList(item_list);
        categorydatas.add(entityCategory);
        //将重构后的数据添加到适配器
         adapter.addCategory(categorydatas);
    }

    @Override
    public void onFailure(Call<EntityCategory> call, Throwable t) {

    }




}
